using System;
using System.Collections.Generic;
using System.Text;
using MCromwell.StaffIntranet.Task.Tasks;
using MCromwell.StaffIntranet.DTO;
using MCromwell.StaffIntranet.DependencyInjection;

namespace MCromwell.StaffIntranet.Presenters
{
    public class LogoutPresenter
    {
        private ILogoutView view;
        private IAuthenticationTask task;

        public LogoutPresenter(ILogoutView view)
            : this(IoC.Resolve<IAuthenticationTask>(), view)
        {
        }

        public LogoutPresenter(IAuthenticationTask task, ILogoutView view)
        {
            if (task == null)
                throw new ArgumentNullException("task", "task cannot be null");

            if (view == null)
                throw new ArgumentNullException("view", "view cannot be null");

            this.task = task;
            this.view = view;
        }


        public void Logout(Guid id)
        {
            NotificationDTO notification = new NotificationDTO();
            SessionInvalidationResult result = task.InvalidateSessionFor(id);

            switch (result)
            {
                case SessionInvalidationResult.Invalidated:
                    notification.AddInfo("user_logged_out", "Successfully logged out");
                    break;
                case SessionInvalidationResult.NotFound:
                    notification.AddError("user_not_logged_in", "Not logged in");
                    break;
            }
            
            view.Display(notification);
        }
    }
}
